package com.tanxi521.tnlevel.utils;

import com.tanxi521.tnlevel.dao.SQLManager;
import com.tanxi521.tnlevel.pojo.PlayerLevel;
import net.md_5.bungee.api.chat.*;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: i-xiaotian
 * @Date: 2022/3/14 22:49
 * @EMAIL: 1254806967@qq.com
 */
public class TopPageHelper {

    public static void topPage(int currentPage, CommandSender sender) {
        int count = SQLManager.selectCount();
        int perPage = ConfigReader.getPerPageCount();
        int totalPage = count % perPage == 0 ? count / perPage : count / perPage + 1;
        List<PlayerLevel> playerLevels = SQLManager.selectTopList(currentPage == 1 ? 0 : (currentPage - 1) * perPage, perPage);
        sender.sendMessage("§a============[等级排名系统start]============");
        int i = currentPage == 1 ? currentPage : (currentPage - 1) * perPage + 1;
        for (PlayerLevel playerLevel : playerLevels) {
            ArrayList<BaseComponent> bc = new ArrayList<>();
            TextComponent tc1 = new TextComponent("§6No. " + i + " §b玩家: " + playerLevel.getDisplayName() + " 等级: " +
                    playerLevel.getPlayerLevel() + " 进度: " + playerLevel.getPlayerExp() +
                    "/" + playerLevel.getPlayerExpToNextLevel() + "    ");
            bc.add(tc1);
            TextComponent tc2 = new TextComponent("点击查看");
            ClickEvent ce = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tnl info " + playerLevel.getDisplayName());
            HoverEvent he = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("§e点击查看" + playerLevel.getDisplayName() + "的详细信息"));
            tc2.setClickEvent(ce);
            tc2.setHoverEvent(he);
            bc.add(tc2);
            BaseComponent[] bcR = new BaseComponent[bc.size()];
            bc.toArray(bcR);
            sender.spigot().sendMessage(bcR);
            i++;
        }
        sender.sendMessage("");
        ArrayList<BaseComponent> bc = new ArrayList<>();
        TextComponent tc1 = new TextComponent("§2点击上一页");
        ClickEvent ce1 = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tnl top " + (currentPage <= 1 ? 1 : currentPage - 1));
        HoverEvent he1 = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("§e点击查看第" + (currentPage <= 1 ? 1 : currentPage - 1) + "页"));
        tc1.setClickEvent(ce1);
        tc1.setHoverEvent(he1);
        bc.add(tc1);
        TextComponent tc3 = new TextComponent("§a  当前为第 " + currentPage + " 页,共 " + totalPage + " 页,总共 " + count + " 名玩家  ");
        bc.add(tc3);
        TextComponent tc2 = new TextComponent("§2点击下一页");
        ClickEvent ce2 = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tnl top " + (currentPage >= totalPage ? totalPage : currentPage + 1));
        HoverEvent he2 = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("§e点击查看第" + (currentPage >= totalPage ? totalPage : currentPage + 1) + "页"));
        tc2.setClickEvent(ce2);
        tc2.setHoverEvent(he2);
        bc.add(tc2);
        BaseComponent[] bcR = new BaseComponent[bc.size()];
        bc.toArray(bcR);
        sender.spigot().sendMessage(bcR);
    }

}
